<?php
/**
 * @package		Joomla.Site
 * @subpackage	com_users
 * @copyright	Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

require_once JPATH_COMPONENT.'/controller.php';

/**
 * Registration controller class for Users.
 *
 * @package		Joomla.Site
 * @subpackage	com_users
 * @since		1.6
 */
class UsersControllerRegistration extends UsersController
{
	/**
	 * Method to activate a user.
	 *
	 * @return	boolean		True on success, false on failure.
	 * @since	1.6
	 */
	public function activate()
	{
		$user		= JFactory::getUser();
		$uParams	= JComponentHelper::getParams('com_users');

		// If the user is logged in, return them back to the homepage.
		if ($user->get('id')) {
			$this->setRedirect('index.php');
			return true;
		}

		// If user registration or account activation is disabled, throw a 403.
		if ($uParams->get('useractivation') == 0 || $uParams->get('allowUserRegistration') == 0) {
			JError::raiseError(403, JText::_('JLIB_APPLICATION_ERROR_ACCESS_FORBIDDEN'));
			return false;
		}

		$model = $this->getModel('Registration', 'UsersModel');
		$token = JRequest::getVar('token', null, 'request', 'alnum');

		// Check that the token is in a valid format.
		if ($token === null || strlen($token) !== 32) {
			JError::raiseError(403, JText::_('JINVALID_TOKEN'));
			return false;
		}

		// Attempt to activate the user.
		$return = $model->activate($token);

		// Check for errors.
		if ($return === false) {
			// Redirect back to the homepage.
			$this->setMessage(JText::sprintf('COM_USERS_REGISTRATION_SAVE_FAILED', $model->getError()), 'warning');
			$this->setRedirect('index.php');
			return false;
		}

		$useractivation = $uParams->get('useractivation');

		// Redirect to the login screen.
		if ($useractivation == 0)
		{
			$this->setMessage(JText::_('COM_USERS_REGISTRATION_SAVE_SUCCESS'));
			$this->setRedirect(JRoute::_('index.php?option=com_users&view=login', false));
		}
		elseif ($useractivation == 1)
		{
			$this->setMessage(JText::_('COM_USERS_REGISTRATION_ACTIVATE_SUCCESS'));
			//$this->setRedirect(JRoute::_('index.php?option=com_users&view=login', false));
			
			// dunglt add
			$items = getArticles(51);
			$slug = $items[0]->id.':'.$items[0]->alias;
			$catslug =$items[0]->catid ? $items[0]->catid .':'.$items[0]->category_alias : $items[0]->catid;
			require_once JPATH_SITE.'/components/com_content/helpers/route.php';
			$readmore = JRoute::_(ContentHelperRoute::getArticleRoute($slug, $catslug));
			$this->setRedirect($readmore);
			//end dunglt
		}
		elseif ($return->getParam('activate'))
		{
			$this->setMessage(JText::_('COM_USERS_REGISTRATION_VERIFY_SUCCESS'));
			$this->setRedirect(JRoute::_('index.php?option=com_users&view=registration&layout=complete', false));
		}
		else
		{
			$this->setMessage(JText::_('COM_USERS_REGISTRATION_ADMINACTIVATE_SUCCESS'));
			$this->setRedirect(JRoute::_('index.php?option=com_users&view=registration&layout=complete', false));
		}
		return true;
	}

	/**
	 * Method to register a user.
	 *
	 * @return	boolean		True on success, false on failure.
	 * @since	1.6
	 */
	public function register()
	{
		// Check for request forgeries.
		JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

		// If registration is disabled - Redirect to login page.
		//echo "ddf" ;die();
		if(JComponentHelper::getParams('com_users')->get('allowUserRegistration') == 0) {
			$this->setRedirect(JRoute::_('index.php?option=com_users&view=login', false));
			return false;
		}

		// Initialise variables.
		$app	= JFactory::getApplication();
		$model	= $this->getModel('Registration', 'UsersModel');

		// Get the user data.
		$requestData = JRequest::getVar('jform', array(), 'post', 'array');

		// Validate the posted data.
		$form	= $model->getForm();
		if (!$form) {
			JError::raiseError(500, $model->getError());
			return false;
		}
		$data	= $model->validate($form, $requestData);
        $data['firma_navn']=JRequest::getVar('firma_navn', '', 'post', '');
        $data['adresse']=JRequest::getVar('adresse', '', 'post', '');
        $data['post']=JRequest::getVar('post', '', 'post', '');
        $data['by_']=JRequest::getVar('by_', '', 'post', '');
        $data['cvr']=JRequest::getVar('cvr', '', 'post', '');
        $data['telefone']=JRequest::getVar('telefone', '', 'post', '');
        
        $data['by_']=JRequest::getVar('by_', '', 'post', '');
        
        $data['name']=JRequest::getVar('name', '', 'post', '');
		$data['lastname']=JRequest::getVar('lastname', '', 'post', '');
        $data['username']=JRequest::getVar('username', '', 'post', '');
        $data['email1']=JRequest::getVar('email', '', 'post', '');
        $data['email2']=JRequest::getVar('email', '', 'post', '');
        $data['password1']=JRequest::getVar('password', '', 'post', '');
        $data['password2']=JRequest::getVar('password', '', 'post', '');
        $data['amount_money']=0;
        
		$joinNewsLetter=JRequest::getVar('joinNewsLetter', '', 'post', '');
	
		
		if($joinNewsLetter=='on' || $joinNewsLetter=='ON' ) {
		
		
			JModel::addIncludePath(JPATH_ROOT.'/components/com_jobs/models', 'myjobs');
			$items_job = JModel::getInstance( 'myjobs', 'JobsModel');
			if(strlen($data['email1']) >0)	
			$items_job->saveEmailNewsletter($data['email1'],$data['email1'], $data['firma_navn']);
			
		}
		//print_r($data);
		
		
        
		//print_r($data);die(); 
		// Check for validation errors.
		/* modify nam */
		if (false) {
		/* ket thuc */
		//if ($data === false) {
			// Get the validation messages.
			$errors	= $model->getErrors();

			// Push up to three validation messages out to the user.
			for ($i = 0, $n = count($errors); $i < $n && $i < 3; $i++) {
				if ($errors[$i] instanceof Exception) {
					$app->enqueueMessage($errors[$i]->getMessage(), 'warning');
				} else {
					$app->enqueueMessage($errors[$i], 'warning');
				}
			}

			// Save the data in the session.
			$app->setUserState('com_users.registration.data', $requestData);

			// Redirect back to the registration screen.
			$this->setRedirect(JRoute::_('index.php?option=com_users&view=registration', false));
			return false;
		}

		// Attempt to save the data.
		//echo "ddf" ;die();
		$return	= $model->register($data);
        
		// Check for errors.
		$session =& JFactory::getSession();		
		if ($return === false) {
			//echo $return;die(); 
			// Save the data in the session.
			$app->setUserState('com_users.registration.data', $data);

			// Redirect back to the edit screen.
			if($model->getError()=='Brugernavn i brug') { 
			
				
			$session->set('firma_navn', $data['firma_navn']);
			$session->set('adresse', $data['adresse']);
			$session->set('post', $data['post']);
			$session->set('firma_navn', $data['firma_navn']);
			$session->set('by_', $data['by_']);
			$session->set('cvr', $data['cvr']);
			$session->set('telefone', $data['telefone']);
			$session->set('by_', $data['by_']);
			$session->set('name', $data['name']);
			$session->set('lastname', $data['lastname']);
			$session->set('email', $data['email1']);
			$session->set('password', $data['password1']);
				
			
			$this->setMessage('<p style="text-align:left;">Den indtastede e-mail-adresse er allerede registreret af en eksisterende
bruger. Indtast venligst en anden e-mail-adresse, eller <a href="'.JRoute::_('index.php?option=com_users&view=login').'">log på</a> med den
allerede oprettede profil.<br/>
Har du glemt adgangskoden, kan du trykke <a href="'.JRoute::_('index.php?option=com_users&view=login&btnfg=1').'">her</a>.</p>');
			
			
			} else {
			$this->setMessage(JText::sprintf('COM_USERS_REGISTRATION_SAVE_FAILED', $model->getError()), 'warning');
			}
			$this->setRedirect(JRoute::_('index.php?option=com_users&view=registration', false));
			return false;
		}
		

		// Flush the data from the session.
		
		$app->setUserState('com_users.registration.data', null);

		// Redirect to the profile screen.
		//echo $return ;die();
		if ($return === 'adminactivate'){
			$this->setMessage(JText::_('COM_USERS_REGISTRATION_COMPLETE_VERIFY'));
			$this->setRedirect(JRoute::_('index.php?option=com_users&view=registration&layout=complete', false));
		} elseif ($return === 'useractivate') {
			//$this->setMessage(JText::_('COM_USERS_REGISTRATION_COMPLETE_ACTIVATE'));
			//$this->setRedirect(JRoute::_('index.php?option=com_users&view=registration&layout=complete', false));
			$items = getArticles(36);
			$slug = $items[0]->id.':'.$items[0]->alias;
			$catslug =$items[0]->catid ? $items[0]->catid .':'.$items[0]->category_alias : $items[0]->catid;
			require_once JPATH_SITE.'/components/com_content/helpers/route.php';
			$readmore = JRoute::_(ContentHelperRoute::getArticleRoute($slug, $catslug));
			//echo $return ;die();
			//$this->setMessage(JText::_('COM_USERS_REGISTRATION_SAVE_SUCCESS'));
			//$this->setRedirect(JRoute::_('index.php?option=com_users&view=login', false));
		    $this->setRedirect($readmore);
		} else {
			$items = getArticles(36);
			$slug = $items[0]->id.':'.$items[0]->alias;
			$catslug =$items[0]->catid ? $items[0]->catid .':'.$items[0]->category_alias : $items[0]->catid;
			require_once JPATH_SITE.'/components/com_content/helpers/route.php';
			$readmore = JRoute::_(ContentHelperRoute::getArticleRoute($slug, $catslug));
			//echo $return ;die();
			//$this->setMessage(JText::_('COM_USERS_REGISTRATION_SAVE_SUCCESS'));
			//$this->setRedirect(JRoute::_('index.php?option=com_users&view=login', false));
		    $this->setRedirect($readmore);
		}


		return true;
	}
}
